package com.xxgc.demo.controller.interceptor;

import com.xxgc.demo.controller.annotations.CheckIdCardData;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CheckIdCardDateInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception{
        //判断handler是否是Method对象
        if(handler instanceof HandlerMethod){
            HandlerMethod method = (HandlerMethod) handler;
            //获取方法上的所有注解，检查是否存在CheckIdCardDate注解
            if(method.getMethodAnnotation(CheckIdCardData.class) != null){
                //☆☆☆☆☆☆☆☆☆☆☆ request的流只能读一次
                ServletInputStream inputStream = request.getInputStream();
                //解决方案 重写request的getInputSteam()方法

                return true;
            }
        }
        return true;
    }
}
